package 课程设计报告_hb_7_6;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import org.junit.Test;

/**
 * @author Jiang Zhiyan
 *
 * 2021年7月6日 上午9:03:55
 *
 */
public class WriterTest {
      @Test
      public void writerTest() throws Exception {
    	  try(
    		   FileOutputStream fos = new FileOutputStream("./demo/d04.txt",true);
    		OutputStreamWriter osw =  new OutputStreamWriter(fos,"utf-8");
    			  ){
//    		  通过字符流写入数据
    		  osw.write("\n");
    		  osw.write("今天下午干嘛？");
    		  osw.flush();
    	  }
      }
      @Test
      public void readTest()throws Exception{
  		try(
  				FileInputStream fis = new FileInputStream("./demo/d04.txt");
  				InputStreamReader osr =  new InputStreamReader(fis,"utf-8");
  				){
  			  //声明一个int类型的数据接受结果
  			int data=-1;
  			//通过while，读取所有的字符
  			while((data = fis.read()) != -1) {
  				System.out.print((char)data);
  			}
  			
  		}
  	}
      @Test
      public void pwTest() throws Exception{
    	  try(FileOutputStream fos= new FileOutputStream("./demo/d05.txt",true);
    			  PrintWriter pw = new PrintWriter(fos,true,StandardCharsets.UTF_8);
    			  ){
    		  pw.println("。。。。");
    	  }
      }
}
